package hai.com.android_test.widget;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import hai.com.android_test.ui.WebViewActivity;

/**
 * Created by 黄海 on 2017/4/26.
 */

public class MyWebChromeClient extends WebChromeClient {
    private static final String TAG = "MyWebChromeClient";
    WebViewActivity activity;
    ValueCallback<Uri[]> filePathCallback;
    ValueCallback<Uri> uploadFile;

    public MyWebChromeClient(WebViewActivity activity) {
        this.activity = activity;
    }

    //for5.0+
    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        Log.d(TAG, "onShowFileChooser() called with: webView = [" + webView + "], filePathCallback = [" + filePathCallback + "], fileChooserParams = [" + fileChooserParams + "]");
        activity.setValueCallback(this.filePathCallback = filePathCallback);
        chooseDialog();
        return true;
    }

    // For android 4.0 - android 4.3
    public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture) {
        Log.d(TAG, "openFileChooser() called with: uploadFile = [" + uploadFile + "], acceptType = [" + acceptType + "], capture = [" + capture + "]");
        activity.setUploadFile(this.uploadFile = uploadFile);
        chooseDialog();
    }

    // android 3.0以上，android4.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        Log.d(TAG, "openFileChooser() called with: uploadMsg = [" + uploadMsg + "], acceptType = [" + acceptType + "]");
        activity.setUploadFile(this.uploadFile = uploadMsg);
        chooseDialog();
    }

    void chooseDialog() {
        String[] items = new String[]{"相册", "相机"};
        final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("请选择").setSingleChoiceItems(items, 0,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (which == 0) {
                            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                            intent.setType("image/*");
                            activity.startActivityForResult(intent.createChooser(intent, "选择图片"), WebViewActivity.CODE_CHOOSER);
                        } else {
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            activity.startActivityForResult(intent, WebViewActivity.CODE_CAMERA);
                        }
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (filePathCallback != null) filePathCallback.onReceiveValue(null);
                if (uploadFile != null) uploadFile.onReceiveValue(null);
            }
        });
        builder.show();
    }
}
